package com.view.utils;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Insets;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import com.controller.MainWindowController;
import com.view.MainWindow;

public class PlaneButton extends JButton {

	private static final long serialVersionUID = -7356133238284516386L;

	public static final int GROUND_PLANE = 0;
	public static final int PLANE_AT_SKY = 1;
	public static final int HIGHLIGHTED_PLANE = 2;
	public static final int TAKE_OFF_PLANE = 3;
	public static final int ROTATING_PLANE = 4;
	public static final int TINY_PLANE = 5;
	public static final int STACK_PLANE = 6;
	public static final int LANDING_PLANE = 7;

	private JPopupMenu menu;
	private long timeLastShown = 0;

	private static final ImageIcon IconoEstacionado;
	private static final ImageIcon IconoSeleccionado;

	static {
		IconoEstacionado = new ImageIcon(
				MainWindow.class.getResource("/images/background/plane.png"));
		IconoSeleccionado = new ImageIcon(
				MainWindow.class
						.getResource("/images/background/plane_hightlight.png"));
	}

	/**
	 * Boton con propiedades.
	 */
	public PlaneButton(final String matricula, int pasajeros, int turbocina,
			int icono, boolean isControlEnabled) {

		super(matricula);

		// Opciones de opacidad
		setForeground(Color.DARK_GRAY);
		setBackground(SystemColor.inactiveCaptionBorder);
		setToolTipText("Matricula: " + matricula + "\nPasajeros: " + pasajeros
				+ "\nCombustible: " + turbocina);
		setVerticalTextPosition(JLabel.BOTTOM);
		setHorizontalTextPosition(JLabel.CENTER);

		setBorder(null);
		setFocusPainted(false);
		setMargin(new Insets(0, 0, 0, 0));
		setContentAreaFilled(false);
		setBorderPainted(false);
		setOpaque(false);
		setFont(new Font("Tahoma", Font.PLAIN, 9));
		setFocusable(false);

		// Se selecciona icono
		switch (icono) {
		case GROUND_PLANE:
			setIcon(IconoEstacionado);
			break;
		case PLANE_AT_SKY:
			setIcon(new ImageIcon(
					MainWindow.class
							.getResource("/images/background/Volaris.gif")));
			break;
		case STACK_PLANE:
			setIcon(new ImageIcon(
					MainWindow.class
							.getResource("/images/background/planeUpDown.png")));

			setHorizontalTextPosition(JLabel.CENTER);
			setVerticalTextPosition(JLabel.CENTER);
			setFont(new Font("Tahoma", Font.BOLD, 11));

			break;
		case TAKE_OFF_PLANE:
			setIcon(new ImageIcon(
					MainWindow.class
							.getResource("/images/background/planeUpDown.png")));
			break;
		case TINY_PLANE:
			setIcon(new ImageIcon(
					MainWindow.class
							.getResource("/images/background/plane_tiny.png")));
			break;
		
		case ROTATING_PLANE:
			setIcon(new ImageIcon(
					MainWindow.class
					.getResource("/images/background/rotate_plane.gif")));
			break;

		case LANDING_PLANE:
			setIcon(new ImageIcon(
					MainWindow.class
					.getResource("/images/background/plane_landing.png")));
			break;

		default:
			break;
		}

		if (isControlEnabled) {
			createActionButton(matricula);
			addMouseListener(new HighlightMark());
		}

	}

	public void createActionButton(final String matricula) {

		setIcon(new ImageIcon(
				MainWindow.class.getResource("/images/background/plane.png")));

		menu = new JPopupMenu();
		menu.addPopupMenuListener(new PopupMenuListener() {

			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				// TODO Auto-generated method stub
				timeLastShown = System.currentTimeMillis();
			}

			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
			}

			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
			}
		});

		addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if ((System.currentTimeMillis() - timeLastShown) > 300) {
					Component c = (Component) e.getSource();
					menu.show(c, c.getWidth() / 2, c.getHeight() / 3);
				}
			}
		});

		JMenuItem retirar = new JMenuItem("Retirar");

		retirar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				MainWindowController.retirePlane(matricula);
			}
		});

		menu.add(retirar);

	}

	class HighlightMark implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			setIcon(IconoSeleccionado);
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			setIcon(IconoEstacionado);
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub

		}

	}
}